using Castle.ActiveRecord;
using Cooking.Core.DataInterfaces;
using Cooking.Core.Model;
using NHibernate.Expression;

namespace Cooking.Data
{
    public class RecipeDao : IRecipeDao
    {
        public Recipe LoadById(int id)
        {
            return ActiveRecordMediator<Recipe>.FindByPrimaryKey(id);
        }

        public void Save(Recipe item)
        {
            ActiveRecordMediator<Recipe>.Update(item);
        }

        public Recipe FindByName(string recipeName)
        {
            DetachedCriteria dc = DetachedCriteria.For<Recipe>()
                .Add(Expression.Eq("Name", recipeName));

            return ActiveRecordMediator<Recipe>.FindFirst(dc);
        }

        public int Create(Recipe recipe)
        {
            ActiveRecordMediator.CreateAndFlush(recipe);
            return recipe.Id;
        }
    }
}